﻿using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;

namespace Downplay.Theory.Economics
{
    public class Migrations : DataMigrationImpl
    {

        public int Create()
        {

            // SOCKETS

            // Product content type
            ContentDefinitionManager.AlterTypeDefinition("Product", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("RoutePart")
                .WithPart("BodyPart")
//                .WithPart("SummaryPart")
  //              .WithPart("PricePart")
                .WithPart("SocketsPart")
                .WithSetting("Stereotype", "Content")
                .Creatable());

            // Category content type
            ContentDefinitionManager.AlterTypeDefinition("Category", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("RoutePart")
                .WithPart("BodyPart")
                .WithPart("SocketsPart")
                .WithSetting("Stereotype", "Content")
                .Creatable());

            // CONNECTORS

            // Products in categories
            ContentDefinitionManager.AlterTypeDefinition("CategoryToProduct", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Category")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Product")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "ProductToCategory")
                    .WithSetting("ConnectorTypePartSettings.DefaultEditorParadigm", "Hidden")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Products in Category")
                    )
                .WithPart("SequencePart")
                .WithSetting("Stereotype", "Content"));

            // Categories from products
            ContentDefinitionManager.AlterTypeDefinition("ProductToCategory", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart",
                part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Product")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Category")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "CategoryToProduct")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Category")
                    )
                .WithSetting("Stereotype", "Content"));

            // Sub categories
            ContentDefinitionManager.AlterTypeDefinition("CategoryToChildCategory", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart",
                part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Category")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Category")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "CategoryToParentCategory")
                    .WithSetting("ConnectorTypePartSettings.AllowMany", "true")
                    .WithSetting("ConnectorTypePartSettings.DefaultEditorParadigm", "Hidden")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Child Categories")
                    )
                .WithPart("SequencePart")
                .WithSetting("Stereotype", "Content"));

            // Parent categories
            ContentDefinitionManager.AlterTypeDefinition("CategoryToParentCategory", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart",
                part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Category")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Category")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "CategoryToChildCategory")
                    .WithSetting("ConnectorTypePartSettings.AllowMany", "false")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Parent Category")
                    )
                .WithSetting("Stereotype", "Content"));

            // Allow displaying categories in sidebar
            ContentDefinitionManager.AlterTypeDefinition("SiteToCategory", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Site")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Category")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "CategoryToSite")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Top Level Categories")
                    )
                // Send category to sidebar when it's connected to Site
                .WithPart("PaperclipPart", part => part
                    .WithSetting("PaperclipTypePartSettings.DefaultPlacement", "AsideFirst:0")
                    .WithSetting("PaperclipTypePartSettings.DefaultDisplayType", "Menu")
                    .WithSetting("PaperclipTypePartSettings.AllowChangePlacement", "true"))
                .WithPart("SequencePart", part => part
                    .WithSetting("SequenceTypePartSettings.EnablePaging", "false"))
                );
            // Allow selecting site from category edit
            ContentDefinitionManager.AlterTypeDefinition("CategoryToSite", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart", part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Category")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Site")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "SiteToCategory")
                    .WithSetting("ConnectorTypePartSettings.AllowMany", "false")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Display on LHS of Site")
                    )
                );
            ContentDefinitionManager.AlterTypeDefinition("ProductToRelatedProduct", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("ConnectorPart",
                part => part
                    .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Product")
                    .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Product")
                    .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "ProductToRelatedProduct")
                    .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Related Products")
                    )
                .WithPart("PaperclipPart", part => part
                        .WithSetting("PaperclipTypePartSettings.DefaultPlacement", "AsideFirst:0")
                        .WithSetting("PaperclipTypePartSettings.DefaultDisplayType", "Thumbnail")
                        .WithSetting("PaperclipTypePartSettings.AllowChangePlacement", "true"))
                .WithSetting("Stereotype", "Content"));

            return 1;
        }
        public int UpdateFrom1()
        {
            // Use a different display type so as not to conflict with navigation
            // Also add missing Stereotypes
            ContentDefinitionManager.AlterTypeDefinition("SiteToCategory", cfg => cfg
                .WithPart("PaperclipPart", part => part
                    .WithSetting("PaperclipTypePartSettings.DefaultDisplayType", "CategoryMenu")
                ).WithSetting("Stereotype", "Content"));
            ContentDefinitionManager.AlterTypeDefinition("CategoryToSite", cfg => cfg
                .WithSetting("Stereotype", "Content"));
            return 2;
        }


    }
}